import geom.Vector;

import java.util.Comparator;


public class Intersect {
	public static final int TOP = Edge.TOP;
	public static final int BTM = Edge.BTM;
	public static final int RIGHT= Edge.RIGHT;
	public static final int LEFT = Edge.LEFT;
	
	public static final int NONE = -1;
	public static final int CLEAR = 0;
	public static final int TOUCH = 1;
	public static final int PART_CLEAR = 2;
	public static final int BLOCKED = 3;
	
	public static final int BEFORE_EDGE = -10;
	public static final int BEGINNING_OF_EDGE = -1;
	public static final int ON_EDGE = 0;
	public static final int END_OF_EDGE = 1;
	public static final int AFTER_EDGE = 10;
	
	int type;
	Edge e1, e2;
	public Vector iPos;
	
	public Intersect(Edge e1, Edge e2){
		type = intersects(e1, e2);
		this.e1 = e1;
		this.e2 = e2;
		if(e1.dir == Edge.HORIZONTAL)
			iPos = new Vector(e2.pos.x, e1.pos.y, 0);
		else 
			iPos = new Vector(e1.pos.x, e2.pos.y, 0);
	}
	
	public static int intersects(Edge e1, Edge e2){
		
		if(e1.dir == e2.dir){
			return NONE;
		}
		else if(e1.dir == Edge.HORIZONTAL){
			if(e1.hilo == Edge.HIGH){
				if(e2.pos.y + e2.l < e1.pos.y) return CLEAR;
				if(e2.pos.y + e2.l == e1.pos.y) return TOUCH;
				else if(e2.pos.y > e1.pos.y) return PART_CLEAR;
				else return BLOCKED;
			}
			else{
				if(e2.pos.y > e1.pos.y) return CLEAR;
				if(e2.pos.y == e1.pos.y) return TOUCH;
				else if(e2.pos.y + e2.l < e1.pos.y) return PART_CLEAR;
				else return BLOCKED;
			}
		}
		else {
			if(e1.hilo == Edge.HIGH){
				if(e2.pos.x + e2.l < e1.pos.x) return CLEAR;
				if(e2.pos.x + e2.l == e1.pos.x) return TOUCH;
				else if(e2.pos.x > e1.pos.x) return PART_CLEAR;
				else return BLOCKED;
			}
			else{
				if(e2.pos.x > e1.pos.x) return CLEAR;
				if(e2.pos.x == e1.pos.x) return TOUCH;
				else if(e2.pos.x + e2.l < e1.pos.x) return PART_CLEAR;
				else return BLOCKED;
			}
		}
	}
	
	public int onEdge(){
		float iVal = iPos.x;
		float ePos = e1.pos.x;
		if(e1.dir == Edge.VERTICAL){
			iVal = iPos.y;
			ePos = e1.pos.y;
		}
		if(iVal < ePos) return BEFORE_EDGE;
		//if(iVal == ePos) return BEGINNING_OF_EDGE;
		//if(iVal == ePos + e1.l) return END_OF_EDGE;
		if(iVal > ePos + e1.l) return BEFORE_EDGE;
		else return ON_EDGE;
		
	}
	

	
	protected static class Comp implements Comparator{
		
		public int compare(Object arg0, Object arg1) {
			Intersect p1 = (Intersect)arg0;
			Intersect p2 = (Intersect)arg1;
			if(p1.e1.dir == Edge.HORIZONTAL)
				return (int)Math.floor(p1.iPos.x - p2.iPos.x);
			else
				return (int)Math.floor(p1.iPos.y - p2.iPos.y);
		}
		
	}
	
	public String toString(){
		StringBuffer sb = new StringBuffer();
		sb.append("Intersect between: \n");
		sb.append(e1.toString());
		sb.append("\n");
		sb.append(e2.toString());
		sb.append("\ntype ");
		sb.append(getTypeString(type));
		sb.append(" at pos: ");
		sb.append(iPos.toString());
		return sb.toString();
	}
	
	public String getTypeString(int i){
		switch(i){
		case -1:
			return "none ";
		case 0:
			return "clear ";
		case 1:
			return "touch ";
		case 2:
			return "part-clear ";
		case 3:
			return "blocked ";
		}
		return "undef ";
	}
}
